package com.icss.ah.sortting.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.j2ee.util.UUID;
/**
 * 
 * @author wj
 *
 */
@Service
@Transactional
public class SorttingService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	
	/**
	 * 修改排序
	 * @param map
	 * @return
	 */
	public boolean changeOrder(HashMap map){
		boolean result = false;
		try{
			int beNum=Integer.parseInt(map.get("beNum").toString()); //修改排序前的值
			if(beNum==0){
				int r = sqlSessionTemplate.update("SorttingBean.changeOrder", map); //修改当前分类的排序
				infolog.info("SorttingService|changeOrder|r:"+r);
				result=true;
			}else{
				int f = sqlSessionTemplate.update("SorttingBean.changeOtherOrder", map); //修改与当前分类交换顺序的分类排序
				infolog.info("SorttingService|changeOrder|f :"+f );
				int s = sqlSessionTemplate.update("SorttingBean.changeOrder", map); //修改当前分类的排序
				infolog.info("SorttingService|changeOrder|s:"+s);
				result=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|changeOrder", e);
		}
		return result;
	}
	
	/**
	 * 分类列表查询
	 * @param map
	 * @return
	 */
	public List getSortList(HashMap map) {
		
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("SorttingBean.querySortList", map);
			infolog.info("SorttingService|getSortList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("SorttingService|getSortList", e);
		}
		return list;
		
	}
	
	/**
	 * 分类列表结果集统计
	 * @param map
	 * @return
	 */
	public Long getSortCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("SorttingBean.querySortCount", map);
			infolog.info("SorttingService|getSortCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|getSortCount", e);
		} 
		return reValue;
	}
	
	/**
	 * 查询当前等级的最大类型编码
	 */
	public String getMaxTypeCode(HashMap map,String pCode) {
		String maxTypeCode = "";
		try {
			if(pCode.equals("")){
				maxTypeCode = (String)sqlSessionTemplate.selectOne("SorttingBean.queryMaxTypeCode", map);//查询一级分类编码最大值
				infolog.info("SorttingService|getMaxTypeCode|maxTypeCode:"+maxTypeCode);
			}else{
				map.put("pCode",pCode);
				maxTypeCode = (String)sqlSessionTemplate.selectOne("SorttingBean.querySendMaxCode", map);//查询二级分类编码最大值
				infolog.info("SorttingService|getMaxTypeCode|maxSecondTypeCode:"+maxTypeCode);
			}
			System.out.println("==========map:"+map);
			String temp = "000";
			if(pCode.length()>=3){
				temp = pCode+"000";
			}
			maxTypeCode=maxTypeCode==null ? temp:maxTypeCode;
			String newMaxCode="";
			try {
				newMaxCode = Integer.parseInt(maxTypeCode)+1 +""; //新增分类编码，编码值为当前最大编码值加一
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			newMaxCode="000"+newMaxCode;
			newMaxCode=newMaxCode.substring(newMaxCode.length()-temp.length(),newMaxCode.length());
			maxTypeCode = newMaxCode;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|getMaxTypeCode", e);
		} 
		return maxTypeCode;
	}
	/**
	 * 查询当前分类级别排序的最大序号
	 */
	public String getMaxSortNum(HashMap map,String pCode) {
		String maxSortNum = "";
		try {
			maxSortNum = (String)sqlSessionTemplate.selectOne("SorttingBean.getMaxSortNum", map);//查询二级分类编码最大值
			infolog.info("SorttingService|getMaxSortNum|getMaxSortNum:"+maxSortNum);
			System.out.println("==========map:"+map);
			maxSortNum=maxSortNum==null ? "0":maxSortNum;
			String newSortNum="";
			try {
				newSortNum = Integer.parseInt(maxSortNum)+1 +""; //新增分类编码，编码值为当前最大编码值加一
			} catch (NumberFormatException e) {
			    e.printStackTrace();
			}
			maxSortNum = newSortNum;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|getMaxSortNum", e);
		} 
		return maxSortNum;
	}
	public boolean saveSort(HashMap map){
		boolean reValue = false;
		try {
			String act = map.get("act").toString();
			String tUuid = map.get("tUuid").toString();
			
			if(act.equals("add")){
				if(tUuid.equals("")){tUuid = new UUID().toString();}
				String pCode = map.get("pCode").toString();
				map.put("level",pCode.length()/3+1);
				String newMaxCode = getMaxTypeCode(map, pCode);
				map.put("newMaxCode",newMaxCode);
				String newMaxSortNum = getMaxSortNum(map, pCode);
				map.put("newMaxSortNum",newMaxSortNum);
				map.put("UUID",tUuid);
				sqlSessionTemplate.insert("SorttingBean.insertSort", map);
			} else {
				sqlSessionTemplate.update("SorttingBean.updateSort", map);
			}
			reValue = true;
			infolog.info("SorttingService|insertSort|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|saveSort", e);
		} 
		return reValue;
	}
	
	/**
	 * 删除一级分类
	 * @param modelKey
	 * @param map
	 * @return
	 */
	public boolean delSort(HashMap map){
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.delete("SorttingBean.delSort", map);
			infolog.info("SorttingService|delSort|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|delSort", e);
		} 
		return reValue;
		
	}

	
	/**
	 * 更新图片
	 * @param map
	 * @return
	 */
	public boolean updatePic(HashMap map){
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("SorttingBean.updatePic", map);
			infolog.info("SorttingService|updatePic|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|updatePic", e);
		} 
		return reValue;
		
	}
	/**
	 * 查询同个公众号，同个商铺id的分类总数
	 * @param map
	 * @return
	 */
	public Long getSortAll(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("SorttingBean.querySortAll", map);
			infolog.info("SorttingService|getSortAll|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("SorttingService|getSortAll", e);
		} 
		return reValue;
	}
	
}
